package zhongbo.java.air.dictionary.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.exception.ExceptionCode;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.IAlertZoneMapper;
import zhongbo.java.air.dictionary.model.AlertZone;
import zhongbo.java.air.dictionary.model.enums.ZoneType;
import zhongbo.java.air.dictionary.service.IAlertZoneService;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;

@Service
public class IAlertZoneServiceImpl implements IAlertZoneService {
    @Autowired
    IAlertZoneMapper iAlertZoneMapper;
    @Override
    public AlertZone insert(AlertZone alertZone) {
        //必不能为空--区域业务编号，多顶点坐标，区域类型
        if(StrUtil.isBlank(alertZone.getZoneCode()) ||
           StrUtil.isBlank(alertZone.getVertices()) ||
           alertZone.getZoneType()==null){
            throw  new RuntimeException("警报区域必填项信息不能为空");
        }
        //列如BJ_NOFLY_001
        if(alertZone.getZoneCode().length()>20){
            throw new RuntimeException("区域业务编码超过限制20字符");
        }

        //禁飞区，限制区，临时区
         if(!Arrays.asList(ZoneType.RESTRICTED,ZoneType.NO_FLY,ZoneType.TEMPORARY).contains(alertZone.getZoneType())){
             throw new RuntimeException("区域类型不符合格式,选择RESTRICTED(限制区),NO_FLY(禁飞区),TEMPORARY(临时区)");
         }
         //查询是否存在zone_code,唯一
         QueryWrapper<AlertZone> queryWrapper =new QueryWrapper<>();
         queryWrapper.eq("zone_code",alertZone.getZoneCode());
         boolean exist = iAlertZoneMapper.exists(queryWrapper);
         if(exist){
             throw new RuntimeException("区域业务编号重复");
         }

         //如果生效开始时间不为空并且生效结束时间不为空
         if(alertZone.getEffectiveEnd()!=null && alertZone.getEffectiveStart()!=null){
             Date start=alertZone.getEffectiveStart();
             Date end =alertZone.getEffectiveEnd();
             if(end.before(start)){
                 throw new RuntimeException("生效结束时间不可以在生效开始时间之前");
             }
         }


        int add = iAlertZoneMapper.insert(alertZone);
        if(add>0){
            return alertZone;
        }
        throw new BusinessException(ExceptionCode.ERROR);

    }

    @Override
    public String delete(String id) {
        AlertZone airPort =iAlertZoneMapper.selectById(id);
        if(airPort==null){
            throw new RuntimeException("删除失败，该信息不存在或者为空");
        }

        iAlertZoneMapper.deleteById(id);
        return id;
    }

    @Override
    public AlertZone select(String id) {
        AlertZone alertZone =iAlertZoneMapper.selectById(id);
          if(alertZone==null){
            throw  new RuntimeException("查到的信息为空，不存在");
          }
        return alertZone;
    }

    @Override
    public AlertZone updateStatus(AlertZone alertZone) {
        //查id
        AlertZone select =iAlertZoneMapper.selectById(alertZone.getId());
        if(select==null){
            throw new RuntimeException("该航迹信息不存在");
        }
        //更新
        int exit =iAlertZoneMapper.updateById(alertZone);
        if(exit>0){
         return alertZone;
        }

        return null;
    }

    @Override
    public PageVo<AlertZone> findPage(String id, Integer page, Integer pageSize) {
        QueryWrapper<AlertZone> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(id)) {
            queryWrapper.like("id", id);
        }
        IPage<AlertZone> pageParam = new Page<>(page, pageSize);
        IPage<AlertZone> airPortIPage = iAlertZoneMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(airPortIPage);
    }


}
